package com.atguigu.Flink.timeAndwindow;

import com.atguigu.Flink.POJO.OrderDetailEvent;
import com.atguigu.Flink.POJO.OrderEvent;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

import java.time.Duration;

public class Flink12_IntervalJoin {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        SingleOutputStreamOperator<OrderEvent> ds1 = env.socketTextStream("hadoop102", 8888)
                .map(
                        line -> {
                            String[] split = line.split(",");
                            return new OrderEvent(split[0], Long.valueOf(split[1]));
                        }
                ).assignTimestampsAndWatermarks(
                        WatermarkStrategy.<OrderEvent>forBoundedOutOfOrderness(Duration.ZERO)
                                .withTimestampAssigner(new SerializableTimestampAssigner<OrderEvent>() {
                                    @Override
                                    public long extractTimestamp(OrderEvent orderEvent, long l) {
                                        return orderEvent.getTs();
                                    }
                                })
                );
        SingleOutputStreamOperator<OrderDetailEvent> ds2 = env.socketTextStream("hadoop102", 9999)
                .map(
                        line -> {
                            String[] split = line.split(",");
                            return new OrderDetailEvent(split[0], split[1], Long.valueOf(split[2]));
                        }
                ).assignTimestampsAndWatermarks(
                        WatermarkStrategy.<OrderDetailEvent>forBoundedOutOfOrderness(Duration.ZERO)
                                .withTimestampAssigner(new SerializableTimestampAssigner<OrderDetailEvent>() {
                                    @Override
                                    public long extractTimestamp(OrderDetailEvent orderDetailEvent, long l) {
                                        return orderDetailEvent.getTs();
                                    }
                                })
                );

        //IntervalJoin
        ds1.keyBy(
                        k -> k.getOrderId()
                ).intervalJoin(
                        ds2.keyBy(
                                k -> k.getOrderId()
                        )
                ).inEventTime()
                .between(Time.seconds(-2), Time.seconds(5))
                .process(
                        new ProcessJoinFunction<OrderEvent, OrderDetailEvent, String>() {
                            @Override
                            public void processElement(OrderEvent orderEvent, OrderDetailEvent orderDetailEvent, ProcessJoinFunction<OrderEvent, OrderDetailEvent, String>.Context context, Collector<String> collector) throws Exception {
                                collector.collect(orderEvent + "--" + orderDetailEvent);
                            }
                        }
                ).print("intervalJoin");


        try {
            env.execute();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
